<?php

class Config extends API_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('config_model');
    }

    public function get() {
        $params = $this->params;
        if (empty($params['fb_id']) == TRUE) {
            $this->response('INVALID_PARAMS');
        }

        $args['fb_id'] = intval($params['fb_id']);
        $key = empty($params['key']) == TRUE ? NULL : $params['key'];

        $result = $this->config_model->get($args, $key);
        if (empty($result) == FALSE) {
            $this->response('GET_SUCCESS', $result);
        } else {
            $this->response('GET_FAIL', $result);
        }
    }

    public function set() {
        $params = $this->params;
        if (empty($params['fb_id']) == TRUE
                OR empty($params['key']) == TRUE
                OR empty($params['value']) == TRUE) {
            $this->response('INVALID_PARAMS');
        }

        $args['fb_id'] = $params['fb_id'];
        $result = $this->config_model->get($args, $params['key']);

        if (empty($result) == TRUE) {
            $data_insert['fb_id'] = intval($params['fb_id']);
            $data_insert['key'] = $params['key'];
            $data_insert['value'] = $params['value'];
            $this->config_model->create($data_insert);
        } else {
            $args['fb_id'] = $params['fb_id'];
            $args['key'] = $params['key'];
            
            $data_update['key'] = $params['key'];
            $data_update['value'] = $params['value'];

            $this->config_model->update($args, $data_update);
        }
        $this->response('CREATE_SUCCESS');
    }

}
